﻿using LPLS.Commons.DBModule;
using LPLS.Commons.Enums;
using LPLS.DBEntities;
using LPLS.DBEntities.ConfigModels.AlarmModels;
using LPLS.DBEntities.ConfigModels.DeviceInfoModels;
using LPLS.DBInterface.ConfigModels.DeviceInfoModels;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LPLS.DBServices.ConfigModels.DeviceInfoModels
{
    public class DeviceBaseInfoService : DBBaseService, IDeviceBaseInfoService
    {
        public DeviceBaseInfoService(DBContext dbContext) : base(dbContext)
        {
        }

        public int AddData(DeviceBaseInfo model)
        {
            model.ID=Guid.NewGuid();

            var result = Client.InsertNav(model)
         .Include(it => it.DeviceComponents, new InsertNavOptions()
         { OneToManyIfExistsNoInsert = true })//配置存在不插入
         .ExecuteCommand();//用例代码
            return result==true?1:0;
        }

        public int DeleteData(DeviceBaseInfo model)
        {
          var  res=  Client.DeleteNav<DeviceBaseInfo>(it => it.ID ==model.ID)
            .Include(z1 => z1.DeviceComponents)
            .ExecuteCommand();

           return res==true?1:0;
        }

        public List<DeviceBaseInfo> GetData()
        {
         
           var data=  Client.Queryable<DeviceBaseInfo>()
                         .Includes(x => x.DeviceComponents)
                         .ToList();

            DealList(data);
            return data;
        }

        public override void InitDataBaseName()
        {
            DataBaseName = DatabaseEnum.SqliteConfig.ToString();
        }

        public int UpdateData(DeviceBaseInfo model)
        {
           return   Client.Updateable(model).ExecuteCommand();

        }


        private void DealList(List<DeviceBaseInfo> li) {


            Parallel.For(0, li.Count, i =>
            {
                li[i].DeviceTypeName = li[i].DeviceType.GetDescription();


                for (int j = 0; j < li[i].DeviceComponents.Count; j++)
                {

                    li[i].DeviceComponents[j].ComponentTypeName = li[i].DeviceComponents[j].ComponentType.GetDescription();

                }

              

            });


        
        }
    }
}
